package com.lyf.language.practice.practice_6_21.io.network;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 1.IP\Socket等基本概念
 * 2.UDP\TCP的基本概念
 * 3.
 * @author LYF
 * @date 2022/6/22
 */
public class TcpUdpTests {
    final private InetAddress inetAddress = InetAddress.getLocalHost();
    //加载构造函数时进行加载该类的熟悉
    public TcpUdpTests() throws UnknownHostException {
    }
    @Test
    public void tcpServer1(){
        ServerSocket socket = null;
        int port = 8888;

        try {
            // serverSocket进行监听，驻扎，并未进行套接字连接，，有点观察者模式
            socket = new ServerSocket(port);
            // 建立套接字连接
            Socket connectSocket = socket.accept();// 阻塞式


            byte[] bytes = new byte[1024];
            int NRead = 0;
            while (true){

                InputStream inputStream = connectSocket.getInputStream();

                while ((NRead=inputStream.read(bytes))!=-1){
                    System.out.println(new String(bytes,0,NRead));

                    // 输出---收到了才反馈
                    OutputStream outputStream = connectSocket.getOutputStream();
                    outputStream.write("ok,has received".getBytes(StandardCharsets.UTF_8));
                    //outputStream.close();
                }

               //inputStream.close();



            }

            // 关闭资源
            //inputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /*
    TCP需要进行连接（三次握手，四次挥手）
     */
    @Test
    public void tcpClient1(){
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1",8888);

            OutputStream outputStream = socket.getOutputStream();

            outputStream.write("hello server".getBytes(StandardCharsets.UTF_8));

            outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket!=null)
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1",8888);

            Scanner scanner = new Scanner(System.in);

            String content;

            while((content = scanner.nextLine()).length()>0){

                OutputStream outputStream = socket.getOutputStream();

                outputStream.write(content.getBytes(StandardCharsets.UTF_8));

                //outputStream.close();
                socket.shutdownOutput();

                InputStream inputStream = socket.getInputStream();

                byte[]bytes =new byte[1024];
                int NRead;
                while((NRead = inputStream.read(bytes))!=-1){
                    System.out.println(new String(bytes,0,NRead));
                }

                //inputStream.close();
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket!=null)
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }

}
